package models;

import java.sql.SQLException;
import java.util.ArrayList;

/** DB persistence **/
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.dao.ForeignCollection;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.misc.RuntimeExceptionBaseDaoEnabled;
import com.j256.ormlite.table.DatabaseTable;

import db.DatabaseHelper;

/**
 * A class to hold information about the current user.
 * @author Shezil Kheraj
 * @version 1.0
 */
@SuppressWarnings("unused")
@DatabaseTable(tableName = "users")
public class User extends RuntimeExceptionBaseDaoEnabled<User, Integer> {

	//Instance Data
	@DatabaseField(generatedId = true)
	private int id;

	@DatabaseField
	private String name;
	
	@DatabaseField
	private String handle;
	
	@DatabaseField
	private String password;
	
	// Non-db backed field for password confirmation
	private String passwordConfirmation;
	
	@DatabaseField
	private String hint;
	
	@DatabaseField
	private String email;
	
	@ForeignCollectionField(eager = true)
    private ForeignCollection<Task> tasks;
	
	public ForeignCollection<Task> getTasks() {
		return tasks;
	}
	
	@ForeignCollectionField(eager = true)
    private ForeignCollection<Category> categories;
	
	private static User currentUser = null;
	
	/**
	 * Getter that returns all the categories
	 * @return categories
	 */
	public ForeignCollection<Category> getCategories() {
		return categories;
	}
	
	// For ORMLite DAO access
	private static RuntimeExceptionDao<User, Integer> defaultDao = null;
	public static void setDefaultDao(RuntimeExceptionDao<User, Integer> runtimeExceptionDao) {
		defaultDao = runtimeExceptionDao;
	}
	
	/**
	 * Getter for the database access object
	 * @return defaultDao
	 */
	public static RuntimeExceptionDao<User, Integer> getDefaultDao() {
		return defaultDao;
	}

	/**
	 * Zero-arg constructor, specifically for ORMlite
	 * @throws SQLException 
	 */
	public User() {
		super();
		setDao((RuntimeExceptionDao<User, Integer>) defaultDao);
	}
	
	/**
	 * This is a constructor for the User class. It initializes all the instance variables.
	 * @param currentUser
	 * @param name
	 * @param handle
	 * @param password
	 * @param hint
	 * @param email
	 */
	public User(String name, String handle, String password, String hint, String email) {
		super();
		setDao((RuntimeExceptionDao<User, Integer>) defaultDao);
		this.name = name;
		this.handle = handle;
		this.password = password;
		this.hint = hint;
		this.email = email;
	}
	
	/**
	 * Getter for currentUser. It returns the Id of the currentUser.
	 * @return currentUser
	 */
	public static User getCurrentUser() {
		return currentUser;
	}

	/**
	 * Setter for currentUser. Stores the ID of the current user into currentUser. 
	 * @param currentUser
	 */
	public static void setCurrentUser(User user) {
		currentUser = user;
	}
	
	public int getId() {
		return id;
	}
	
	/**
	 * Getter for name. It returns the string name.
	 * @return name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Setter for name. It stores the parameter into the name string.
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Getter for handle. It returns the handle string.
	 * @return handle
	 */
	public String getHandle() {
		return handle;
	}

	/**
	 * Setter for handle. It stores the parameter into the handle string.
	 * @param handle
	 */
	public void setHandle(String handle) {
		this.handle = handle;
	}

	/**
	 * Getter for password. It returns the password string
	 * @return password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * Setter for password. It stores the parameter in to the password string.
	 * @param password
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * Getter for Hint. It returns the hint string
	 * @return hint
	 */
	public String getHint() {
		return hint;
	}

	/**
	 * Setter for hint. It stores the parameter into the hint string. 
	 * @param hint
	 */
	public void setHint(String hint) {
		this.hint = hint;
	}

	/**
	 * Getter for Email. Returns the email String.
	 * @return email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * Setter for Email String. The parameter is stored in email.
	 * @param email
	 */
	public void setEmail(String email) {
		this.email = email;
	} 
	
//	/**
//	 * Getter for user. It returns all the users.
//	 * @return allUsers
//	 */
//	public static ArrayList<User> getAllUsers(){
//		return allUsers;
//	}
	
	/**
	 * Convert User to a String object
	 * @return Stringified version
	 */
	public String toString() {
		return id+":"+name+":"+handle+":"+password;
	}
}
